﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using Abp.Runtime.Caching;
using AutoMapper.QueryableExtensions;
using FwProject.AppVsrsion.Dto;
using FwProject.Car.Dto;
using FwProject.Core.AppVersion;
using FwProject.Manager;
using X.PagedList;

namespace FwProject.AppVersion
{
    public class AppVersionAppService : FwProjectAppServiceBase, IAppVersionAppService
    {
        public readonly IRepository<AppVersionManger, Guid> _repositoryAppVersion;
        private readonly AppVersionInfoManager _appVersionManager;
        public readonly ICacheManager _catchmanager;
        public IGuidGenerator _guid { get; set; }
        public IObjectMapper _map { get; set; }

        public AppVersionAppService(ICacheManager catchmanager, IRepository<AppVersionManger, Guid> repositoryAppVersion,
            AppVersionInfoManager appVersionManager)
        {
            _catchmanager = catchmanager;
            _repositoryAppVersion = repositoryAppVersion;
            _appVersionManager = appVersionManager;
        }
        /// <summary>
        /// app版本管理
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Guid> Create(AppVersionInputDto input)
        {
            var model = _map.Map<AppVersionManger>(input);
            model.Id = _guid.Create();
            return await _appVersionManager.CreateAppVersion(model);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task EditAppVersion(Guid id, AppVersionInputDto input)
        {
            var model = await _repositoryAppVersion.GetAsync(id);
            model = _map.Map(input, model);
            await _appVersionManager.UpdateAppVersion(model);
        }
        /// <summary>
        /// 获取一个版本信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AppVersionDto> GetByIdAsync(Guid id)
        {
            var model = (await GetCache).Where(o => o.Id == id).FirstOrDefault();
            return _map.Map<AppVersionDto>(model);
        }
        /// <summary>
        /// 获取版本列表
        /// </summary>
        /// <param name="pageIdx"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<IPagedList<AppVersionDto>> GetPagedListAsync(int pageIdx, int pageSize)
        {
            var listmodel = (await GetCache).AsQueryable();
            return await listmodel.ProjectTo<AppVersionDto>().ToPagedListAsync(pageIdx, pageSize);
        }

        public async Task<AppVersionDto> GetAppVersionAsync()
        {
            var model = (await GetCache).AsQueryable().FirstOrDefault();
            return _map.Map<AppVersionDto>(model);
        }

        private Task<List<AppVersionManger>> GetCache
        {
            get
            {
                ICache cache = _catchmanager.GetCache(nameof(AppVersionManger));
                return cache.GetAsync("list", async () => await _repositoryAppVersion.GetAllListAsync());
            }

        }
    }
}
